package com.xizi.forum.service.impl;

import com.xizi.forum.common.Constants;
import com.xizi.forum.common.ResultCode;
import com.xizi.forum.dao.MessageInfoMapper;
import com.xizi.forum.exception.ApplicationException;
import com.xizi.forum.model.MessageInfo;
import com.xizi.forum.service.IMessageInfoService;
import com.xizi.forum.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
@Slf4j
public class MessageInfoServiceImpl implements IMessageInfoService {
    @Autowired
    private MessageInfoMapper messageInfoMapper;

    /**
     * 创建站内信
     * @param message
     * @return
     */
    @Override
    public Boolean create(MessageInfo message) {
        //1.参数校验
        if(message.getReceiveUserId() == null || message.getReceiveUserId() < 0 || StringUtil.isEmpty(message.getContent())){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        //2.sql校验
        int row = messageInfoMapper.insertSelective(message);
        if(row != 1){
            log.error(ResultCode.FAILED_SERVICE.toString());
            throw new ApplicationException(ResultCode.FAILED_SERVICE);
        }
        return true;
    }

    /**
     * 获取站内信未读的数量
     * @param id 当前登入用户id
     * @return
     */
    @Override
    public Long selectUnreadCount(Long id) {
        if(id == null || id < 0){
            throw new ApplicationException(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        Long count = messageInfoMapper.selectUnreadCount(id);
        log.info("当前登入用户未读的站内信数量为：" + count + ",userId = " + id);
        return count;
    }

    /**
     * 获取所有站内信
     * @param id 当前登入用户id
     * @return
     */
    @Override
    public List<MessageInfo> getAllMessageByUserId(Long id) {
        //1.参数校验
        if(id == null || id < 0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new  ApplicationException(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        List<MessageInfo> list = messageInfoMapper.selectAllMessageByUserId(id);
        return list;
    }

    @Override
    public MessageInfo selectById(Long id) {
        if(id == null || id < 0){
            throw new  ApplicationException(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        MessageInfo messageInfo = messageInfoMapper.selectByPrimaryKey(id);

        return messageInfo;
    }

    /**
     * 修改站内信状态
     * @param id 当前站内信id
     * @return
     */
    @Override
    public void updateStateById(Long id, Byte state) {
        //1.参数校验
        if(id == null || id < 0 || state == null || state < 0 || state > 2){
            throw new  ApplicationException(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        //2.创建更新站内信对象
        MessageInfo messageInfo = new MessageInfo();
        messageInfo.setId(id);
        messageInfo.setState(state);

        int row = messageInfoMapper.updateByPrimaryKeySelective(messageInfo);
        if(row != 1){
            throw new ApplicationException(ResultCode.FAILED_SERVICE);
        }
    }
    /**
     * 站内信回复
     * @param repliedId 回信用户id
     * @param message 信
     */
    @Override
    public void reply(Long repliedId, MessageInfo message) {
        //1. 参数校验
        if(repliedId == null || repliedId <= 0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        // 更新当前站内信状态，为已回复
        updateStateById(repliedId, (byte) 2);
        // 创建回复的站内信
        create(message);
    }


    /**
     * 通过id删除站内信（逻辑删除）
     * @param id
     * @return
     */
    @Override
    public Boolean deleteById(Long id) {
        //1.参数校验
        if(id == null || id <= 0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new  ApplicationException(ResultCode.FAILED_PARAMS_VALIDATE);
        }

        //2.如果站内信处于未读状态不能删除
        MessageInfo messageInfo = messageInfoMapper.selectByPrimaryKey(id);
        if(messageInfo == null || messageInfo.getDeleteState() == 1){
            log.error(ResultCode.FAILED_NOT_EXISTS.toString());
            throw new ApplicationException(ResultCode.FAILED_SERVICE);
        }
        if(messageInfo.getState() == Constants.USER_STATE_CAN_SEND_MESSAGE){
            log.info(ResultCode.FAILED_MESSAGE_NO_READ.toString());
            throw new ApplicationException(ResultCode.FAILED_MESSAGE_NO_READ);
        }
        //3. 构造 要删除的站内信 对象
        MessageInfo updateMessageInfo = new MessageInfo();
        updateMessageInfo.setId(id);
        updateMessageInfo.setDeleteState((byte) 1);
        int row = messageInfoMapper.updateByPrimaryKeySelective(updateMessageInfo);

        if(row != 1){
            log.error("sql 异常： " +  ResultCode.ERROR_SERVICES);
            throw new ApplicationException(ResultCode.ERROR_SERVICES);
        }

        return true;
    }
}
